package com.vision.admin.cl.ui.component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.IdentityValueProvider;
import com.sencha.gxt.core.client.Style.SelectionMode;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.grid.CheckBoxSelectionModel;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.grid.GridView;
import com.vision.admin.cl.AdminServices;
import com.vision.admin.cl.resource.AdminResources;
import com.vision.core.cl.Callback;
import com.vision.core.cl.ui.UI;
import com.vision.core.cl.ui.component.BasicGridView;
import com.vision.core.cm.data.CompanyProperties;
import com.vision.core.cm.db.data.Company;

/**
 * 
 * @author Mark
 *
 */
public class CompanyListWindow implements IsWidget {
	
	interface Binder extends UiBinder<Widget, CompanyListWindow> { }
	
	private static final Binder binder = GWT.create(Binder.class);
	
	@UiField Window window;
	
	@UiField Grid<Company> grid;
	
	@UiField TextButton okBtn;
	@UiField TextButton cancelBtn;

	List<Company> selected;

	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	public Widget asWidget() {
		window = (Window) binder.createAndBindUi(this);
		window.getHeader().setIcon(AdminResources.INSTANCE.companies());
		
		return window;
	}
	
	// UiFactory Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@UiFactory
	Grid<Company> initializeGrid() {
		CompanyProperties props = CompanyProperties.INSTANCE;
		
		IdentityValueProvider<Company> identity = new IdentityValueProvider<Company>();
		CheckBoxSelectionModel<Company> sm = new CheckBoxSelectionModel<Company>(identity);
		sm.setSelectionMode(SelectionMode.MULTI);

		List<ColumnConfig<Company, ?>> columns = new ArrayList<ColumnConfig<Company, ?>>();

		columns.add(sm.getColumn());
		columns.add(UI.column(props.code(), 80, "Code"));
		columns.add(UI.column(props.name(), 200, "Name"));

		ColumnModel<Company> cm = new ColumnModel<Company>(columns);

		final Grid<Company> grid = new Grid<Company>(new ListStore<Company>(props.key()), cm);
		grid.setAllowTextSelection(false);
		grid.setSelectionModel(sm);
		
		GridView<Company> view = new BasicGridView<Company>();
		view.setAutoExpandColumn(cm.findColumnConfig(props.name().getPath()));
		view.setEmptyText("There are no companies to display.");
		grid.setView(view);
		
		return grid;
	}
	
	// Getters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	public List<Company> getSelected() {
		return selected; 
	}
	
	public void loadData(final List<Company> toNotInclude) {
		window.mask("Loading companies...");
		AdminServices.companySvc().getAll(new Callback<List<Company>>(window) {
			@Override
			protected void handleSuccess(List<Company> result) {
				result.removeAll(toNotInclude);
				
				grid.getStore().clear();
				grid.getStore().addAll(result);
			}
		});
	}
	
	// Logic %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@UiHandler("okBtn")
	void saveClicked(SelectEvent e) {
		selected = grid.getSelectionModel().getSelectedItems();
		
		window.hide(okBtn);
	}
	
	@UiHandler("cancelBtn")
	void cancelClicked(SelectEvent e) {
		selected = Collections.emptyList();
		
		window.hide(cancelBtn);
	}

}
